package com.eastfair.imaster.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.imaster.dao.ConferenceTopicSpeakerMapper;
import com.eastfair.imaster.dto.ConferenceTopicSpeakerDTO;
import com.eastfair.imaster.entity.ConferenceTopicSpeaker;
import com.eastfair.imaster.enumeration.ConferenceTopicSpeakerTypeEnum;
import com.eastfair.imaster.mydao.MyConferenceTopicSpeakerMapper;
import com.eastfair.imaster.service.ConferenceTopicSpeakerService;
import com.eastfair.imaster.vo.ConferenceTopicSpeakerVO;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 业务实现类
 * 论坛主题嘉宾
 * </p>
 *
 * @author zjn
 * @date 2021-08-31
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class ConferenceTopicSpeakerServiceImpl extends SuperServiceImpl<ConferenceTopicSpeakerMapper, ConferenceTopicSpeaker> implements ConferenceTopicSpeakerService {

    @Autowired
    ProjectCoreServiceFeign projectCoreServiceFeign;
    @Autowired
    private MyConferenceTopicSpeakerMapper myConferenceTopicSpeakerMapper;


    /**************sharding 分库分表操作 *****************/
    @Override
    public boolean updateByIdOfShard(ConferenceTopicSpeaker entity) {
        UpdateWrapper<ConferenceTopicSpeaker> updateWrapper = new UpdateWrapper<>();
        //可将指定字段更新为null
        updateWrapper.lambda().eq(ConferenceTopicSpeaker::getProjectId, entity.getProjectId()).eq(ConferenceTopicSpeaker::getId, entity.getId());
        entity.setProjectId(null);
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(ConferenceTopicSpeaker model) {
        //雪花ID
        model.setId(DefaultUidGenerator.getUidOfProjectId(ContextUtil.getProjectId()));
        model.setProjectId(ContextUtil.getProjectId());
        return R.successDef();
    }

    @Override
    public R<Boolean> saveConferenceTopicSpeakers(List<ConferenceTopicSpeakerDTO> conferenceTopicSpeakerDTOList, Long conferenceTopicId, ConferenceTopicSpeakerTypeEnum conferenceTopicSpeakerTypeEnum) {
        if (Objects.isNull(conferenceTopicSpeakerDTOList) || conferenceTopicSpeakerDTOList.isEmpty() || Objects.isNull(conferenceTopicSpeakerTypeEnum)) {
            return R.fail(ExceptionCode.ERROR_NULL_PARAMS);
        }

        // 批量生成雪花id
        List<Long> ids = batGetSelfUid(conferenceTopicSpeakerDTOList.size());

        // 论坛主题嘉宾入库
        List<ConferenceTopicSpeaker> conferenceTopicSpeaker = ConvertUtil.convertList(conferenceTopicSpeakerDTOList, ConferenceTopicSpeaker.class);
        for (int i = 0; i < conferenceTopicSpeaker.size(); i++) {
            conferenceTopicSpeaker.get(i).setProjectId(ContextUtil.getProjectId()).setConferenceTopicId(conferenceTopicId)
                    .setType(conferenceTopicSpeakerTypeEnum).setId(ids.get(i));
        }
        return R.success(saveBatch(conferenceTopicSpeaker));
    }

    @Override
    public R<Boolean> deleteByConferenceTopicId(Long conferenceTopicId, ConferenceTopicSpeakerTypeEnum conferenceTopicSpeakerTypeEnum) {
        if (conferenceTopicId == null) {
            return R.fail(ExceptionCode.ERROR_NULL_PARAMS);
        }

        UpdateWrapper<ConferenceTopicSpeaker> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ConferenceTopicSpeaker::getProjectId, ContextUtil.getProjectId()).eq(ConferenceTopicSpeaker::getConferenceTopicId, conferenceTopicId)
                .set(ConferenceTopicSpeaker::getIsDeleted, BusinessConstant.DELETE_YES);
        if (conferenceTopicSpeakerTypeEnum != null) {
            updateWrapper.lambda().eq(ConferenceTopicSpeaker::getType, conferenceTopicSpeakerTypeEnum.name());
        }
        return R.success(update(updateWrapper));
    }

    @Override
    public R<List<ConferenceTopicSpeakerVO>> getByConferenceTopicId(Long conferenceTopicId, ConferenceTopicSpeakerTypeEnum conferenceTopicSpeakerTypeEnum, String sortOrderType) {
        if (Objects.isNull(conferenceTopicId) || Objects.isNull(conferenceTopicSpeakerTypeEnum == null)) {
            return R.fail(ExceptionCode.ERROR_NULL_PARAMS);
        }
        return R.success(myConferenceTopicSpeakerMapper.getByConferenceTopicId(conferenceTopicId, conferenceTopicSpeakerTypeEnum.getCode(), sortOrderType));
    }

    @Override
    public R<Boolean> deleteAndSaveByConferenceTopicId(List<ConferenceTopicSpeakerDTO> conferenceTopicSpeakerDTOList, Long conferenceTopicId, ConferenceTopicSpeakerTypeEnum conferenceTopicSpeakerTypeEnum) {
        if (Objects.isNull(conferenceTopicId) || Objects.isNull(conferenceTopicSpeakerTypeEnum) || CollectionUtil.isEmpty(conferenceTopicSpeakerDTOList)) {
            return R.fail(ExceptionCode.ERROR_NULL_PARAMS);
        }

        // 删除嘉宾
        deleteByConferenceTopicId(conferenceTopicId, conferenceTopicSpeakerTypeEnum);

        //添加嘉宾
        return saveConferenceTopicSpeakers(conferenceTopicSpeakerDTOList, conferenceTopicId, conferenceTopicSpeakerTypeEnum);
    }

    /**
     * 批量生成雪花id
     *
     * @param size
     * @return
     */
    private List<Long> batGetSelfUid(Integer size) {
        List<Long> ids = DefaultUidGenerator.getBatUidOfProjectId(ContextUtil.getProjectId(), size);
        if (ids == null || ids.isEmpty()) {
            throw new RuntimeException("System exception, generate snowflake id error");
        }
        return ids;
    }
}
